package br.ita.redecasd.mi.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.icu.util.StringTokenizer;

import br.ita.redecasd.mi.ws.SessionHandlerWSProxy;

/**
 * Servlet implementation class for Servlet: SignValidatorServlet
 *
 */
 public class SignValidatorServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 4692955347654947509L;

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public SignValidatorServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
        {
            String username = request.getParameter("username");
            String sigString = request.getParameter("signature");
            if ( !sigString.equals( "Não consegui validar a assinatura" ))
            {
            	
            	java.lang.String userAgent = request.getHeader( "User-Agent" );
            	//br.ita.redecasd.mi.signvalidatorproxy.SignValidatorWS port = service.getSignValidatorWSPort();
            	SessionHandlerWSProxy proxy = new SessionHandlerWSProxy();
                
            	byte[] uasigned = restoreSignature(sigString);
            	String result = proxy.validateSign(username, userAgent, uasigned);
            	if ( result != null  )
            	{
            		request.getSession().setAttribute("username", username );
            		request.getSession().setAttribute( "sessionID", result );
            		RequestDispatcher view = request.getRequestDispatcher("welcome.jsp");
            		view.forward(request, response);
            	}
            	else
            	{
            		request.getSession().setAttribute("message", "Não foi possível validar a assinatura digital.");
            	}
            	
            }
        }
        catch( Exception e )
        {
        	request.getSession().setAttribute("message", "Não foi possível validar a assinatura digital.");
        }
        RequestDispatcher view = request.getRequestDispatcher("index.jsp");
        view.forward(request,response);
        
	} 
	
	private byte[] restoreSignature( String sigInStr )
	{
		StringTokenizer st = new StringTokenizer(sigInStr, ";");
		byte[] sign_in_bytes = new byte[st.countTokens()];
		for ( int i = 0; i < sign_in_bytes.length; i++ )
			sign_in_bytes[i] = Byte.parseByte(st.nextToken());
		
		return sign_in_bytes;
	}
}